package com.qigao.mall.service.impl;

import com.qigao.mall.api.JobLogService;
import com.qigao.mall.commons.enums.JobTypeEnum;
import com.qigao.mall.jdbc.mapper.TbJobLogMapper;
import com.qigao.mall.jdbc.model.TbJobLog;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author luiz
 * @Title: com.qigao.mall.service.impl
 * @ProjectName market-parent
 * @date 2020-07-2020-07-21 14:02
 * @Description:
 */
@Service
public class JobLogServiceImpl implements JobLogService {
    @Resource
    private TbJobLogMapper tbJobLogMapper;
    @Override
    public TbJobLog execBefore(Date execDate, JobTypeEnum jobTypeEnum) {
        TbJobLog tbJobLog=tbJobLogMapper.selectByIndex(execDate,jobTypeEnum.getCode());
        if(tbJobLog==null){
             tbJobLog=new TbJobLog();
            tbJobLog.setExecDate(execDate);
            tbJobLog.setJobType(jobTypeEnum.getCode());
            tbJobLog.setJobName(jobTypeEnum.getName());
            tbJobLog.setStartTime(new Date());
            tbJobLog.setExecStatus(0);
            tbJobLogMapper.insertReturnKey(tbJobLog);
            return tbJobLog;
        }
        return tbJobLog;
    }

    @Override
    public void execAfter(TbJobLog tbJobLog) {
        if(tbJobLog==null)return;
        tbJobLog.setExecStatus(1);
        tbJobLog.setEndTime(new Date());
        tbJobLogMapper.updateByPrimaryKeySelective(tbJobLog);
    }
}
